﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _3D_Processor
{
    public partial class Feedback : Form
    {
        public Feedback()
        {
            InitializeComponent();
            regarding.SelectedIndex = 0;
        }

        private void CancleButton_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void ContinueButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(fullname.Text))
            {
                MessageBox.Show("Your name is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!validators.IsValidEmail(email.Text))
            {
                MessageBox.Show("A valid e-mail address is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(message.Text))
            {
                MessageBox.Show("Message can not be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (SendEmail("", "", regarding.Items[regarding.SelectedIndex].ToString(), fullname.Text + ": " + email.Text + " wrote <br/><br/>" + message.Text.Replace("\n", "<br/>")))//ommited for security reason
            {
                MessageBox.Show("Thank you, your message has been sent successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            return;
        }


        public bool SendEmail(string to, string from, string subject, string data)
        {
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to);
            mail.Subject = subject;
            mail.Body = data;
            mail.IsBodyHtml = true;

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("", 587);//ommited for security reason
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            System.Net.NetworkCredential credential = new System.Net.NetworkCredential("", "");//ommited for security reason
            smtp.Credentials = credential;
            try
            {
                smtp.Send(mail);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message+" I am sorry, I am unable to send your message now. Please try again shortly.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

    }
}
